package org.pet.controller;

import org.pet.entity.Pet;
import org.pet.service.PetService;
import org.pet.util.PageBean;
import org.pet.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private PetService petService;

    /**
     * 根据id获取宠物
     * @param id 宠物id
     * @return
     */
    @GetMapping("/getInfoById")
    public Result<Pet> getInfoById(@RequestParam Integer id){
        Pet pet = petService.getInfoById(id);
        if (pet == null){
            return Result.error("宠物不存在");
        }
        return Result.success(pet);
    }

    /**
     * 根据category获取宠物
     * @param category 宠物种类
     * @return
     */
    @GetMapping("/getInfoByCategory")
    public Result<Pet> getInfoByCategory(@RequestParam String category){
        Pet pet = petService.getInfoByCategory(category);
        if (pet == null){
            return Result.error("宠物不存在");
        }
        return Result.success(pet);
    }

    /**
     * 返回分页的数据列
     * @param pageNum 当前页码
     * @param pageSize  分页大小
     * @param category  根据宠物种类筛选（可以没有）
     * @return
     */
    @GetMapping("/listPage")
    public Result<PageBean<Pet>> listPage(Integer pageNum,
                                          Integer pageSize,
                                          @RequestParam(required = false) String category
                                          ){
        PageBean<Pet> list = petService.listPage(pageNum,pageSize,category);
        return Result.success(list);
    }

    @PutMapping("/add")
    public Result add(@RequestBody @Validated Pet pet){
        return petService.add(pet);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id){
        return petService.delete(id);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated Pet pet){
        return petService.update(pet);
    }
}
